﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp.Tools.Utils;

namespace WpfApp.View.LineManage
{
    /// <summary>
    /// WD_InsertStationManage.xaml 的交互逻辑
    /// </summary>
    public partial class WD_InsertStationManage : Window
    {
        public WD_InsertStationManage()
        {
            InitializeComponent();
        }
        DataTable dt;//临时表格
        BLL.UCStationManage.UC_StationManageClient myStationManage = new BLL.UCStationManage.UC_StationManageClient();
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region 绑定省份信息
            DataTable dtProvince = myStationManage.SelectProvince().Tables[0];
            cbo_pro.ItemsSource = dtProvince.DefaultView;
            cbo_pro.DisplayMemberPath = "proName";
            cbo_pro.SelectedValuePath = "proID";
            #endregion
            //绑定表格数据
            dt = myStationManage.SelectStationManage().Tables[0];
            dgSite.ItemsSource = dt.DefaultView;
        }
        /// <summary>
        /// 重置按钮（重置数据）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Reset_Click(object sender, RoutedEventArgs e)
        {
            txt_full_code.Text = string.Empty;
            txt_short_code.Text = string.Empty;
            txt_Station.Text = string.Empty;
            cbo_pro.SelectedValue = 0;
            dgSite.ItemsSource = dt.DefaultView;
        }
        /// <summary>
        /// 保存按钮（新增数据）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Affirm_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                using (TransactionScope scope = new TransactionScope())
                {
                
                    if (cbo_pro.SelectedValue != null && !string.IsNullOrEmpty(txt_Station.Text) && !string.IsNullOrEmpty(txt_short_code.Text)
                   && !string.IsNullOrEmpty(txt_full_code.Text))
                    {
                    //获取页面数据
                    string strsite_name = txt_Station.Text.ToString().Trim();
                    string strshort_code = txt_short_code.Text.ToString().Trim();
                    string strfull_code = txt_full_code.Text.ToString().Trim();
                    int intpro_id = Convert.ToInt32(cbo_pro.SelectedValue);
                    Boolean blstop_no = false;
                    
                    //执行站点新增：新增站点表
                    DataTable resules = myStationManage.InsertStation(strsite_name, strshort_code, strfull_code, intpro_id, blstop_no).Tables[0];
                    //判断返回数据行resules.Rows.Count
                    if (resules.Rows.Count > 0)
                    {
                        //获取单元格（站点ID）
                        int intsiteID = Convert.ToInt32(resules.Rows[0][0].ToString());
                        int intNeighborCount = 0;
                        //循环新增（邻居站点信息）
                        for (int i = 0; i < dgSite.Items.Count; i++)
                        {
                            if (Convert.ToBoolean(dt.Rows[i]["chked"]) == true && ((DataRowView)dgSite.Items[i]).Row["distance"].ToString() !="")
                            {
                                //执行新增邻居站点(neighborSiteID)
                                int neighborSiteID = Convert.ToInt32(((DataRowView)dgSite.Items[i]).Row["siteID"]);
                                decimal distance = Convert.ToDecimal(((DataRowView)dgSite.Items[i]).Row["distance"]);//
                                //int siteID, int neighborSiteID, string distance
                                intNeighborCount = Convert.ToInt32(myStationManage.InsertNeighborSite(intsiteID, neighborSiteID, distance));
                            }
                        }
                        //判断是否执行成功
                        if (intNeighborCount > 0 || resules.Rows.Count>0)
                        {
                            MessageBoxResult dr = MessageBox.Show("新增站点数据成功！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);//弹出确定对话框
                            if (dr == MessageBoxResult.OK)
                            {
                                this.Close();
                            }
                        }
                        else
                        {
                            MessageBox.Show("新增站点数据失败！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                        }
                    }

                }
                else
                {
                    MessageBox.Show("请把数据填写完整！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
                }

            }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex);
                MessageBox.Show("数据异常！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Error);
            }
        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult dr = MessageBox.Show("退出页面数据将不保留。", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);//弹出确定对话框
            if (dr == MessageBoxResult.OK)//如果点了确定按钮
            {
                //关闭窗口
                this.Close();
            }
        }
        /// <summary>
        /// 站点文本改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Station_SelectionChanged(object sender, RoutedEventArgs e)
        {
            txt_short_code.Text = PublicStaticMothd.GetChineseSpell(txt_Station.Text.Trim());//引用拼音简码方法 
            txt_full_code.Text = (new PublicStaticMothd.PinYin()).GetABC(txt_Station.Text);
        }

        private void dgSite_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

        }
    }
}
